package org.eclipse.epsilon.emc.spreadsheets.csv.store;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.eclipse.epsilon.common.util.FileUtil;
import org.eclipse.epsilon.emc.spreadsheets.SpreadsheetRow;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvModel;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvRow;
import org.eclipse.epsilon.emc.spreadsheets.csv.CsvWorksheet;
import org.junit.Test;

public class StoreFileTest
{
	private static CsvModel model = null;

	private void setUp() throws Exception
	{
		model = new CsvModel();
		model.setFile("resources/store/StoreFileTest.txt");
		model.setConfigurationFile("resources/store/StoreFileTestConfig.xml");
		model.setHasHeader(true);
		model.setStoredOnDisposal(false);
		model.load();
	}

	@Test
	public void testStoreRows() throws Exception
	{
		FileUtil.setFileContents("c0,c1,c2,c3,c4,c5" + System.getProperty("line.separator"), new File(
				"resources/store/StoreFileTest.txt"));
		this.setUp();

		Map<String, Object> values = new HashMap<String, Object>();
		values.put("c_0", "value0");
		values.put("c_1", "value1");
		values.put("c_2", "value2");
		values.put("c_3", "value3");
		values.put("c_4", "value4");
		values.put("c_5", "value5");
		model.createInstance(CsvWorksheet.NAME, values);
		model.createInstance(CsvWorksheet.NAME, values);
		model.store();

		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isNotEmpty(rows));
		assertTrue(rows.size() == 2);
		for (int i = 0; i < 2; i++)
		{
			CsvRow row = (CsvRow) rows.get(i);
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c0")).equals("value0"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c1")).equals("value1"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c2")).equals("value2"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c3")).equals("value3"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c4")).equals("value4"));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c5")).equals("value5"));
			row.getModel().deleteElement(row);
		}
		model.store();

		rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isEmpty(rows));
	}

	@Test
	public void createEmptyRows() throws Exception
	{
		FileUtil.setFileContents("c0,c1,c2,c3,c4,c5" + System.getProperty("line.separator"), new File(
				"resources/store/StoreFileTest.txt"));
		this.setUp();

		Map<String, Object> values = new HashMap<String, Object>();
		values.put("c_0", "");
		values.put("c_1", "");
		values.put("c_2", "");
		values.put("c_3", "");
		values.put("c_4", "");
		values.put("c_5", "");
		model.createInstance(CsvWorksheet.NAME, values);
		model.createInstance(CsvWorksheet.NAME, values);
		model.createInstance(CsvWorksheet.NAME, values);
		model.store();

		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isNotEmpty(rows));
		assertTrue(rows.size() == 3);
		for (int i = 0; i < 3; i++)
		{
			CsvRow row = (CsvRow) rows.get(i);
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c0")).equals(""));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c1")).equals(""));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c2")).equals(""));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c3")).equals(""));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c4")).equals(""));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c5")).equals(""));
			row.getModel().deleteElement(row);
		}
		model.store();

		rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isEmpty(rows));
	}

	@Test
	public void createEmptyRowsWithEOL() throws Exception
	{
		String header = "c0,c1,c2,c3,c4,c5" + System.getProperty("line.separator");
		String firstLine = ",,,,," + System.getProperty("line.separator");
		FileUtil.setFileContents(header + firstLine, new File("resources/store/StoreFileTest.txt"));
		this.setUp();

		Map<String, Object> values = new HashMap<String, Object>();
		values.put("c_0", "");
		values.put("c_1", "");
		values.put("c_2", "");
		values.put("c_3", "");
		values.put("c_4", "");
		values.put("c_5", "");
		model.createInstance(CsvWorksheet.NAME, values);
		model.createInstance(CsvWorksheet.NAME, values);
		model.createInstance(CsvWorksheet.NAME, values);
		model.store();

		List<SpreadsheetRow> rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isNotEmpty(rows));
		assertTrue(rows.size() == 4);
		for (int i = 0; i < 4; i++)
		{
			CsvRow row = (CsvRow) rows.get(i);
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c0")).equals(""));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c1")).equals(""));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c2")).equals(""));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c3")).equals(""));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c4")).equals(""));
			assertTrue(row.getVisibleCellValue(row.getWorksheet().getColumn("c5")).equals(""));
			row.getModel().deleteElement(row);
		}
		model.store();

		rows = (List<SpreadsheetRow>) model.getAllOfKind(CsvWorksheet.NAME);
		assertTrue(CollectionUtils.isEmpty(rows));
	}

}
